package com.sinodata.bsm.cicp.collector.jboss;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.management.ObjectName;

import org.apache.log4j.Logger;
import org.jboss.jmx.adaptor.rmi.RMIAdaptor;

import com.sinodata.bsm.cicp.collector.Collector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.cicp.util.RmiUtil;
import com.sinodata.bsm.common.vo.ResPropertyValue;

/**
 * 
 * <p>
 * Description: Jboss Manager各项指标的采集
 * </p>
 *
 * @author wangpeng
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-7-24 下午09:50:06          wangpeng        1.0         To create
 * </p>
 *
 * @since 
 * @see
 */
public class ManagerCollector extends Collector {
    private static Logger logger = Logger.getLogger(ManagerCollector.class);

    @Override
    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        List<ResPropertyValue> values = new ArrayList<ResPropertyValue>();
        String[] propMetas = getPropMeta();
        ResPropertyValue propertyValueX = new ResPropertyValue();
        ResPropertyValue.PropValue[] propValues = new ResPropertyValue.PropValue[propMetas.length];
        //        String getManagerName = this.getParamMap().get("名称").trim();
        String getManagerName = this.getParamMap().get("Name").trim();
        try {
            //            RMIAdaptor rmiAdapter = RmiUtil.getRmiAdaptor(ip, Integer.parseInt(this.getParamMap().get("端口")));
            RMIAdaptor rmiAdapter = RmiUtil.getRmiAdaptor(ip, Integer.parseInt(this.getParamMap().get("Port")));
            Set<ObjectName> objectNames = RmiUtil.queryObjectNames(rmiAdapter, "jboss.web:type=Manager,*");
            for (ObjectName jbossManager : objectNames) {
                if (getManagerName.equals(jbossManager.toString())) {
                    String sessionCounter = rmiAdapter.getAttribute(jbossManager, "sessionCounter") + "";
                    String activeSessions = rmiAdapter.getAttribute(jbossManager, "activeSessions") + "";
                    String rejectedSessions = rmiAdapter.getAttribute(jbossManager, "rejectedSessions") + "";
                    String sessionAverageAliveTime = rmiAdapter.getAttribute(jbossManager, "sessionAverageAliveTime") + "";
                    String maxActive = rmiAdapter.getAttribute(jbossManager, "maxActive") + "";
                    String maxActiveSessions = rmiAdapter.getAttribute(jbossManager, "maxActiveSessions") + "";

                    propValues[0] = ResPropertyValue.createPropValue(sessionCounter, propMetas[0]);
                    propValues[1] = ResPropertyValue.createPropValue(activeSessions, propMetas[1]);
                    propValues[2] = ResPropertyValue.createPropValue(rejectedSessions, propMetas[2]);
                    propValues[3] = ResPropertyValue.createPropValue(sessionAverageAliveTime, propMetas[3]);
                    propValues[4] = ResPropertyValue.createPropValue(maxActive, propMetas[4]);
                    propValues[5] = ResPropertyValue.createPropValue(maxActiveSessions, propMetas[5]);

                    propertyValueX.setValues(propValues);
                    values.add(propertyValueX);
                    return values;
                }
            }
        } catch (Exception e) {
            logger.error(e);
            throw new CollectorException(e);
        }

        return null;
    }

    @Override
    public String[] getParamMeta() {
        //        return new String[] { "端口", "名称" };
        return new String[] { "Port", "Name" };
    }

    @Override
    public String[] getPropMeta() {
        //        return new String[] { "会话总量", "活动会话数", "拒绝会话数", "平均会话活动时长", "最大并发会话数", "最大并发数限制" };
        //        return new String[] { "TotalSessions", "ActiveSessionsNum", "RefuseSessionsNum", "AvgSessionActiveTimeLong", "MaxConcurrentSessionsNum", "MaxConcurrentNumLimit" };
        return new String[] { "$1", "$2", "$3", "$4", "$5", "$6" };
    }
}
